fix: RouteNotFound handler does not falls back to root one#2859
Open
majiayu000 wants to merge 2 commits intolabstack:masterfrom
Open
fix: RouteNotFound handler does not falls back to root one#2859majiayu000 wants to merge 2 commits intolabstack:masterfrom
majiayu000 wants to merge 2 commits intolabstack:masterfrom
Conversation
…iddleware When a group has middleware, the RouteNotFound handler registered at the root level is now properly used as a fallback. Previously, groups with middleware would always use the default NotFoundHandler instead of falling back to the root's custom RouteNotFound handler. This fix: - Adds a routeNotFoundHandler field to Echo to store the root handler - Modifies group.Use() to use a fallback handler that checks for the root's RouteNotFound handler at request time - Updates tests to reflect the new expected behavior Fixes labstack#2485 Signed-off-by: majiayu000 <1835304752@qq.com>
Contributor
|
Hi, happy new year! This is interesting idea but I think you can achieve same by replacing default NotFoundHandler (assuming you do not deal with multiple echo instances which routes/groups are added in mixed order) echo.NotFoundHandler = func(c echo.Context) error {
return echo.NewHTTPError(http.StatusNotFound, "custom route not found")
}
e.RouteNotFound("*", echo.NotFoundHandler)
g := e.Group("/group") // <--- will use replaced echo.NotFoundHandler the root problem/cause here is that we have these 404 handlers added. Lines 27 to 33 in d0f9d1e Which is something we can not change but if we are looking for workaround I think replacing the 404handler is probably the easiest way. |
Contributor
|
also these group level 404 routes can be replaced my404 := func(c echo.Context) error {
return echo.NewHTTPError(http.StatusNotFound, "custom route not found")
}
g := e.Group("/group")
g.RouteNotFound("", my404)
g.RouteNotFound("/*", my404)
echo.NotFoundHandler = my404
e.RouteNotFound("*", my404) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2485
Changes